<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\services\activity\score;

use app\dao\activity\score\StoreScoreDao;
use app\dao\activity\score\StoreScoreTaskDao;
use app\model\activity\score\StoreScoreTaskRecord;
use app\services\activity\score\StoreScoreTaskRecordServices;
use app\services\BaseServices;
use app\services\order\StoreOrderServices;
use app\services\user\UserBillServices;
use app\services\user\UserServices;
use crmeb\exceptions\AdminException;

/**
 * Class StoreIntegralOrderServices
 * @package app\services\order
 * @method getOrderIdsCount(array $ids) 获取订单id下没有删除的订单数量
 * @method getUserOrderDetail(string $key, int $uid) 获取订单详情
 * @method getBuyCount($uid, $type) 获取用户已购买此活动商品的个数
 */
class StoreScoreTaskServices extends BaseServices
{
    /**
     * StoreIntegralOrderServices constructor.
     * @param StoreScoreTaskDao $dao
     */
    public function __construct(StoreScoreTaskDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTaskList()
    {
        $list = $this->dao->getList();
        return compact('list');
    }

    /**
     * @param int $task_id
     * @param array $user
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function finish(int $task_id, array $user = [])
    {
        $task = $this->dao->getTask($task_id);
        if (empty($task)) {
            throw new AdminException(400803);
        }

        $StoreScoreTaskRecordServices = app()->make(StoreScoreTaskRecordServices::class);
        $task_record = $StoreScoreTaskRecordServices->count(["task_id" => $task_id, "uid" => $user['uid']]);

        if ($task['finish_num'] <= $task_record) {
            throw new AdminException(400804);
        }

        switch ($task['finish_type']) {
            case 1://无法判断，点击即完成
                switch ($task['type']){
                    case 3:
                        $count = StoreScoreTaskRecord::where("task_id",$task_id)
                            ->where('uid',$user['uid'])
                            ->where('add_time','>',strtotime(date('Y-m-d')))
                            ->count();
                        if($count >= 3){
                            throw new AdminException("任务无法重复完成");
                        }
                        break;
                    default:
                        break;
                }
                break;
            case 2://商品数
                $StoreOrderServices = app()->make(StoreOrderServices::class);
                $total_num = $StoreOrderServices->getProductCount($user['uid']);
                if ($total_num < $task['total_num']) {
                    throw new AdminException(400805);
                }
                break;
            default:
                throw new AdminException(400578);
        }
        // 增加积分
        $bill = [];
        $bill['uid'] = $user['uid'];
        $bill['title'] = '完成任务';
        $bill['mark'] = '完成[' . $task['title'] . ']积分任务';
        $bill['number'] = $task['score'];
        $bill['balance'] = $user['integral'] + $task['score'];
        $bill['add_time'] = time();
        $userBillServices = app()->make(UserBillServices::class);
        $userBillServices->incomeIntegral($user['uid'], 'task', $bill);
        // 积分记录
        $edit = [];
        $edit['integral'] = bcadd($user['integral'], $task['score'], 2);
        $userServices = app()->make(UserServices::class);
        $userServices->dao->update($user['uid'], $edit);
        // 任务记录
        $record = [
            'uid' => $user['uid'],
            'task_id' => $task_id,
        ];
        $StoreScoreTaskRecordServices->save($record);
    }
}
